/**
 * 
 */
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Hashtable;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




import Utils.Constant;
import Utils.DbAccess;
import Utils.FileUtils;
import Utils.GenUtils;
import Utils.ImageUtils;
import Utils.Random;

import Utils.ZipResponse;
import Utils.upload.FileUpload;
import Utils.upload.Section;






public class UploadHomePic extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 4194452284080389577L;
	
	DbAccess db = null;
	
	public void init() {
		try {
			db = new DbAccess(Constant.DB,true);
		} catch ( Exception e) {
			e.printStackTrace();
		}
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		process(req,res);
	}

	private void process(HttpServletRequest req, HttpServletResponse res) throws IOException {
		res.setContentType("text/html");
		req.setCharacterEncoding("UTF-8");
		res.setCharacterEncoding("UTF-8");
//		if(!CheckLogin.check(req)){
//			ZipResponse.write(
//				req,
//				res,
//				"<html><script>window.location='blank.htm';window.parent.onAddNews('false','');</script></html>",
//				"UTF-8");
//			return;
//		}
		Connection conn = null;
		PreparedStatement stmt = null;
		String folder1 = "";
		try{
			conn = db.getConn();
			String rootPath = req.getRealPath("/");
			if (!rootPath.endsWith("/"))
				rootPath += "/";
			folder1 =rootPath + "WEB-INF/uploads/news/"+Random.randomString(3);
			File f = new File(folder1);
			if (!f.exists())
				f.mkdirs();
			FileUpload fileUpload = new FileUpload(f.getAbsolutePath(), 0);
			Hashtable uploads = null;
			uploads = fileUpload.getUpload(req, res);
			String newsId = ((Section) uploads.get("newsId")).getValue().trim();
			String oldfile = ((Section) uploads.get("oldFile")).getValue().trim();
			String folder = GenUtils.genPathByAccountId(newsId);
			
			String img = ImageUtils.uploadHomePic(oldfile,uploads, folder);
			
			if(!img.equals("")){
				String sql = "UPDATE news SET HomePic=? WHERE NewsId=?";
				stmt = conn.prepareStatement(sql);
				stmt.setString(1, img);
				stmt.setString(2, newsId);
				if(stmt.executeUpdate()>0){

				}
			}else{
				ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onUpload('true','');</script></html>",
				"UTF-8");
				return;
			}
		}catch (Exception e) {
			e.printStackTrace();
			ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onUpload('true','');</script></html>",
			"UTF-8");
		}finally{
			if(stmt!=null){try{stmt.close();}catch (Exception e) {}}
			if(conn!=null){try{conn.close();}catch (Exception e) {}}
			FileUtils.deleteDir(new File(folder1));
		}
		ZipResponse.write(req,res,"<html><script>window.location='blank.htm';window.parent.onUpload('true','');</script></html>",
				"UTF-8");
	}
	
}
